package com.szholly.data.coderule;

import java.util.List;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.szholly.utils.util.StringUtils;
import com.szholly.data.coderule.rules.*;
import com.szholly.data.coderule.table.*;
import com.szholly.data.general.*;

/*
 * 定义编码规则实现类
 */
@Component("RuleClass")
@Scope("prototype")
public class RuleClass implements IRule, BeanFactoryAware {

	private BeanFactory beanFactory;

	public void setBeanFactory(BeanFactory beanFactory)
			throws org.springframework.beans.BeansException {
		this.beanFactory = beanFactory;
	}

	public BeanFactory getBeanFactory() {
		return beanFactory;
	}

	// / <summary>
	// / 获取编码规则
	// / </summary>
	// / <param name="ruleID">编码规则编号</param>
	// / <param name="table">具体表</param>
	// / <param name="entity">数据实体</param>
	// / <param name="refEntity">参考实体对象</param>
	// / <returns></returns>
	public String GetCodeRule(String ruleID, ITable table, RowBase entity,
			RowBase refEntity) {
		if (StringUtils.IsNullOrSpace(ruleID))
			return "";

		List<RuleInfoRow> entityList = RuleInfoTable.getSingleRef()
				.GetRowByRuleID(ruleID);
		if (entityList == null || entityList.size() <= 0)
			return "";

		String ruleCode = "";
		for (RuleInfoRow ruleEntity : entityList) {
			ICodeRule codeRule = (ICodeRule) this.getBeanFactory().getBean(
					"CodeRule_" + ruleEntity.getTYPE());

			if (codeRule == null)
				continue;
			String infoID = (String) ruleEntity.getValue(GlobalDataType.String, "OBJECTID");
			infoID = "'" + infoID + "'";
			String codeRuleItem = codeRule.GetCodeRule(table, entity,
					ruleEntity.getPARAS(), refEntity, ruleCode, infoID);
			if (StringUtils.IsNullOrSpace(codeRuleItem))
				continue;

			if (ruleEntity.getLENGHT() > 0) {
				char fillChar = '0';
				if (!StringUtils.IsNullOrSpace(ruleEntity.getFILLCHAR()))
					fillChar = ruleEntity.getFILLCHAR().charAt(0);
				if (ruleEntity.getFILLTYPE() == FillType.PadLeft) {
					codeRuleItem = StringUtils.PadLeft(codeRuleItem,
							ruleEntity.getLENGHT(), fillChar);
				} else {
					codeRuleItem = StringUtils.PadRight(codeRuleItem,
							ruleEntity.getLENGHT(), fillChar);
				}
			}

			ruleCode += codeRuleItem;
		}
		return ruleCode;
	}

	// / <summary>
	// / 确定是否可以编码
	// / </summary>
	// / <param name="ruleID">编码规则编号</param>
	// / <param name="table">具体表</param>
	// / <param name="entity">数据实体对象</param>
	// / <param name="refEntity">参考实体对象</param>
	// / <returns></returns>
	public Boolean NeedCodeRule(String ruleID, ITable table, RowBase entity,
			RowBase refEntity) {
		if (StringUtils.IsNullOrSpace(ruleID))
			return false;

		List<RuleInfoRow> entityList = RuleInfoTable.getSingleRef()
				.GetRowByRuleID(ruleID);
		if (entityList == null || entityList.size() <= 0)
			return false;

		for (RuleInfoRow ruleEntity : entityList) {
			ICodeRule codeRule = (ICodeRule) this.getBeanFactory().getBean(
					"CodeRule_" + ruleEntity.getTYPE());
			if (codeRule == null)
				continue;
			if (codeRule.NeedCodeRule(table, entity, ruleEntity.getPARAS(),
					refEntity, ruleEntity.getNEEDFIELDS()))
				return true;
		}
		return false;
	}
}
